﻿using Core.Context;
using Core.Dtos;
using Core.Factories;
using Core.IServices;
using Core.Messages;
using Core.Queries;
using Front.Controllers.Api;
using Library.Logging;
using Share.Enterprise.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Front.Areas.Admin.Controllers.Api
{
    [RoutePrefix("api/admin/cache")]
    //[Authorize(Roles = "Admin")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class CacheApiController : ApiController, IQueryApiController<string, CacheEntryDto, CacheQuery>
    {
        private readonly IManageCacheService service;
        private readonly IRequestFactory requestFactory;
        private readonly ICurrentContext currentContext;

        public CacheApiController(IManageCacheService service, ICurrentContext currentContext, IRequestFactory requestFactory, ILogger logger)           
        {
            this.service = service;
            this.requestFactory = requestFactory;
            this.currentContext = currentContext;
        }

        [HttpPost]
        [Route("searches")]
        public QueryResponse<CacheEntryDto> FindBy(CacheQuery query)
        {
            Request<CacheQuery> request = requestFactory.GetInstance<CacheQuery>(query);
            return service.FindBy(request);
        }

        [Route("{id}")]
        public CacheEntryDto Get(string id)
        {
            return service.FindByID(id);
        }

        [HttpDelete]
        [Route("{id}")]
        public Response Delete(string id)
        {
            return service.Delete(id);
        }
    }
}
